<?php

global $_W, $_GPC;
include 'function.php';

$ops = array('list', 'teacher_post', 'pic_delete', 'teacher_throw', 'teacher_delete');
$op  = in_array($_GPC['op'], $ops) ? $_GPC['op'] : 'list';

if ($op == 'list') {

    $type = isset($_GPC['type']) ? $_GPC['type'] : "0";

    $pageIndex = max(1, intval($_GPC['page']));
    $pageSize  = 10;

    $keywords = safe_gpc_string($_GPC['teacher_name']);

    $sql   = "SELECT * FROM " . tablename('dgjy_teacher');
    $where = " WHERE uniacid=:uniacid AND teacher_pid = 0 ";

    //筛选咨询老师或者业务老师
    if (!empty($type)) {//暂时不区分老师
        if ($type == 3) {//放入回收站的老师
            $where = " WHERE uniacid=:uniacid AND teacher_pid = 1 ";
        } else {
            $where = $where . " AND teacher_type = " . $type;
        }
    }

    //关键字查找
    if (!empty($keywords)) {
        $where = $where . " AND teacher_name LIKE '%" . $keywords . "%' ";
    }

    $total      = pdo_fetchcolumn("SELECT count(*) FROM" . tablename('dgjy_teacher') . $where, array(':uniacid' => $_W['uniacid']));
    $pagination = pagination($total, $pageIndex, $pageSize);

    $sql = $sql . $where . " ORDER BY teacher_id DESC LIMIT " . ($pageIndex - 1) * $pageSize . "," . $pageSize;
//    var_dump($sql);
    $teacher = pdo_fetchall($sql, array(':uniacid' => $_W['uniacid']));

    include $this->template('teacher/teacher');
}

if ($op == 'teacher_post') {
    $_W['page']['title'] = '编辑老师';

    $query = load()->object('query');
    $user  = $query->from('dgjy_user')->where('uniacid', $_W['uniacid'])->getall();

    $teacher_id = intval($_GPC['teacher_id']);
    $teacher    = $query->from('dgjy_teacher')->where('teacher_id', $teacher_id)->get();
//    var_dump($teacher);
    $isnew = false;
    if (empty($teacher)) {//不存在则新建
        $isnew   = true;
        $teacher = array(
            'teacher_type' => 1,
        );
    }

    if (checksubmit('submit')) {
        $openid = $_GPC['openid'];
        if ($isnew) {//添加老师
            $isbind = $query->from('dgjy_teacher')->where('openid', $openid)->get();//查询该id是否绑定了老师
            if ($isbind) {
                message('该会员已经绑定为其他老师,请先解除绑定', '', 'error');
                return;
            }
        } else {//不是新增即编辑的时候
            if ($teacher['openid'] != $openid) {//当出现openid不同的情况,表示,换绑定的老师了,所以要查看id是否绑定了其他老师
                $isbind = $query->from('dgjy_teacher')->where('openid', $openid)->get();//查询该id是否绑定了老师
                if ($isbind) {
                    message('该会员已经绑定为其他老师,请先解除绑定', '', 'error');
                    return;
                }
            }
        }
        if (empty($_GPC['teacher_avatar'])) {
            $avatar = $_GPC['avatar_old'];
        } else {
            $avatar = $_GPC['teacher_avatar'];
        }

        $data = array(
            'uniacid'           => $_W['uniacid'],
            'openid'            => $_GPC['openid'],
            'teacher_type'      => $_GPC['teacher_type'],
            'teacher_name'      => $_GPC['teacher_name'],
            'teacher_specialty' => $_GPC['teacher_specialty'],
            'teacher_intro'     => $_GPC['teacher_intro'],
            'teacher_avatar'    => $avatar,
            'createtime'        => $_W['timestamp'],
        );
        if ($isnew) {//新的,新增
            $res = pdo_insert('dgjy_teacher', $data);
        } else {//否则更新
            $res = pdo_update('dgjy_teacher', $data, array('teacher_id' => $teacher_id));
        }
        if (!empty($res)) {
            message($isnew ? '恭喜，添加成功！' : '恭喜，编辑成功！', $this->createWebUrl('teacher', array('op' => 'list')), 'success');
        } else {
            message($isnew ? '抱歉，添加失败！' : '抱歉，编辑失败！', '', 'error');
        }
    }

    include $this->template('teacher/teacher_add');
}

if ($op == 'teacher_throw') {//放入回收站
    $id = intval($_GPC['teacher_id']);
    if (!empty($id)) {
        $query   = load()->object('query');
        $teacher = $query->from('dgjy_teacher')->where('uniacid', $_W['uniacid'])->where('teacher_id', $id)->get();//根据id查找该老师
        if (!empty($teacher)) {
            if ($teacher['uniacid'] == $_W['uniacid']) {
                $res = pdo_update('dgjy_teacher', array('teacher_pid' => 1), array('uniacid' => $_W['uniacid'], 'teacher_id' => $id));
                if (!empty($res)) {
                    itoast('已放入回收站', referer(), 'success');
                } else {
                    itoast('抱歉！操作失败！', referer(), 'error');
                }
            } else {
                message('抱歉！您没有权限修改此信息！', $this->createWebUrl('teacher'), 'error');
            }
        } else {
            message('抱歉！您要修改的教师信息不存在！', $this->createWebUrl('teacher'), 'error');
        }
    } else {
        message('警告！请勿提交非法网址！', $this->createWebUrl('teacher'), 'error');
    }
}

if ($op == 'teacher_delete') {
    $id = intval($_GPC['teacher_id']);
    if (!empty($id)) {
        $query   = load()->object('query');
        $teacher = $query->from('dgjy_teacher')->where('uniacid', $_W['uniacid'])->where('teacher_id', $id)->get();//根据id查找该老师
        if (!empty($teacher)) {
            if ($teacher['uniacid'] == $_W['uniacid']) {
                $res = pdo_delete('dgjy_teacher', array('uniacid' => $_W['uniacid'], 'teacher_id' => $id));//删除老师
                if (!empty($res)) {
                    itoast('恭喜！删除成功！', referer(), 'success');
                } else {
                    itoast('抱歉！删除失败！', referer(), 'error');
                }

            } else {
                itoast('抱歉！您没有权限删除此信息！', $this->createWebUrl('teacher'), 'error');
            }
        } else {
            message('抱歉！您要删除的信息不存在！', $this->createWebUrl('teacher'), 'error');
        }
    } else {
        message('警告！请勿提交非法网址！', $this->createWebUrl('teacher'), 'error');
    }
}

?>